package com.daka.controller;

import com.daka.annotation.Login;
import com.daka.entity.User;
import com.daka.entity.Welfare;
import com.daka.entity.WelfareItem;
import com.daka.enumerate.DictionaryMap;
import com.daka.enumerate.StatusCode;
import com.daka.service.FileUtil;
import com.daka.service.WelfareItemService;
import com.daka.tools.CheckLogin;
import com.daka.vo.response.BaseResponseVo;
import com.daka.vo.response.ListDataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author beliveli on 2016/3/31 0031.
 */
@Controller
@RequestMapping("/welfareItem")
@Slf4j
public class WelfareItemController {

    @Resource(name = "welfareItemService")
    private WelfareItemService welfareItemService;

    @Resource
    private FileUtil fileUtil;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/list", consumes = "application/json")
    @Login
    public ListDataResponseVo list(@Valid @RequestBody Welfare welfare, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.SYSTEM.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new ListDataResponseVo<User>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        List<WelfareItem> welfareItems = welfareItemService.selectByWelfareId(welfare.getId(), welfare.getCode());
        return new ListDataResponseVo<WelfareItem>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), welfareItems);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/upload", headers = "content-type=multipart/*")
    public BaseResponseVo upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

        String dir = request.getSession().getServletContext().getRealPath("/upload");// + Constants.UPLOADDIR;

        File dest = fileUtil.uploadFile(file, dir);

        try {
            importBrandPeriodSort(dest);
        } catch (Exception e) {
            log.error("上传失败 ： " + e.getMessage());
            return new BaseResponseVo(StatusCode.UPLOAD_ERROR.getStatusCode(), StatusCode.UPLOAD_ERROR.getStatusMessage());
        }

        return new BaseResponseVo(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage());
    }


    private List<WelfareItem> importBrandPeriodSort(File excelFile) throws Exception {

        List<WelfareItem> welfareItemList = fileUtil.readExcelFile(excelFile);
        welfareItemService.insertBatch(welfareItemList);
        return welfareItemList;
    }
}
